<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="author" content="featon inc.">
    <link rel="stylesheet" href="/static/vendor/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/vendor/bootstrap/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="/static/vendor/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/static/vendor/jqPagination/css/jqpagination.css">
    <link rel="stylesheet" href="/static/vendor/datetimepicker/jquery.datetimepicker.css">
    <link rel="stylesheet" href="/static/vendor/skin/ftskin.css">
    <title>HelloWorld</title>
</head>
<body>
<div class="ft-subpage-wrapper container-fluid">
    <div class="row">
        <div class="col-lg-12">
            <div class="tools-box">
                <div class="form-inline">
                    <div class="form-group">
                        <button type="button" class="btn btn-info" onclick="openAddPage()"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 添加</button>
                        <button type="button" class="btn btn-warning" onclick="updateRecord(table,'role_id','openUpdatePage()');"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span> 修改</button>
                        <button type="button" class="btn btn-danger" onclick="deleteRecord(table,'role_id','deleteObject()');"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> 删除</button>

                    </div>

                    <span style="float:right;display:none;">
									<table style="text-align:left;padding-right:5px;float:right" cellspacing="0" cellpadding="0" border="0">
										<tr>
											<td width="195">
												<div class="input-group">
													<input type="text" class="form-control" placeholder="关键字">
													<span class="input-group-btn">
														<button class="btn btn-info" type="button"><span class="glyphicon glyphicon-search" aria-hidden="true"></span> 查询</button>
													</span>
												</div>
											</td>
										</tr>
									</table>
								</span>
                </div>

            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-12" id="table">

        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->

    <div class="row">
        <div id="turn">

        </div>
    </div>
    <!-- /.row -->

</div>
<script src="/static/vendor/jquery/jquery-3.3.1.min.js"></script>
<script src="/static/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="/static/vendor/jqPagination/js/jquery.jqpagination.min.js"></script>
<script src="/static/vendor/skin/js/ft-ui.js"></script>
<script src="/static/vendor/skin/js/table-common.js"></script>
<script src="/static/vendor/skin/js/java.js"></script>
<script src="/static/vendor/skin/js/jquery.c.js"></script>
<script>
    var tp = new TurnPage();
    var table = new Table();
    table.table_name = "table_list";
    var beanList = null;
    var con_map = new Map();

    jQuery(function($){
        initTable();
        loadListble()
    });

    function initTable(){

        var colsMap = new Map();
        var colsList = new List();

        colsList.add(setTitleClos("role_name","角色名称","200px","","",""));//英文名，显示名，宽，高，样式名，点击事件　
        colsList.add(setTitleClos("action_col","操作","300px","","",""));
        colsList.add(setTitleClos("rele_shared","应用范围","100px","","",""));
        colsList.add(setTitleClos("space_col"," ","","","",""));

        table.setColsList(colsList);
        table.setAllColsList(colsList);
        table.enableSort=false;//禁用表头排序
        table.onSortChange = showList;
        table.show("table");//里面参数为外层div的id
    }

    function loadListble()
    {
        showList();
        showTurnPage();
    }

    function showList(){
        con_map.put("start_num", tp.getStart());
        con_map.put("page_size", tp.pageSize);

        beanList = top.ajax.synclBody("/role/getRoleList",con_map.toJSON())

        curr_bean = null;
        table.setBeanList(beanList,"td_list");//设置列表内容的样式
        table.show("table");



        table.getCol("action_col").each(function(i){
            if(i>0)
            {
                $(this).html('<a href="javascript:openUpdatePage(\''+beanList[i-1].role_id+'\')">编辑</a>&nbsp;&nbsp;<a href="javascript:deleteSingleObject(\''+beanList[i-1].role_id+'\')">删除</a>&nbsp;&nbsp;<a href="javascript:openReleOperateHandl('+(i-1)+',true)">关联权限</a>&#160;&#160;<a href="javascript:openReleUserHandl('+(i-1)+')">关联用户</a>');
            }
        });

        table.getCol("rele_shared").each(function(i){
            if(i>0)
            {
                switch(beanList[i-1].rele_shared)
                {
                    case 0:$(this).text("系统角色");break;
                    case 1:$(this).text("业务角色");break;
                    case 2:$(this).text("养殖场");break;
                    case 3:$(this).text("屠宰场");break;
                    case 4:$(this).text("诊疗机构");break;
                }
            }
        });

        table_input_init(table.table_name);
    }

    function showTurnPage(){
        tp.total = top.ajax.synclBody("/role/getRoleCount",con_map.toJSON())
        tp.show("turn","");
        tp.onPageChange = showList;
    }

    function openAddPage()
    {
        modelWindow.openFrameWindow("维护角色","/role/go_role_add",600,330);
    }

    function openUpdatePage(id)
    {
        var sel_id = '';
        if(id != '' && id != null)
        {
            sel_id = id;
        }else
            sel_id = table.getSelecteCheckboxValue("role_id");
        modelWindow.openFrameWindow("维护角色","/role/go_role_add?role_id="+sel_id,600,330);
    }

    function deleteObject(id)
    {
        var sel_id = '';
        if(id != '' && id != null)
        {
            sel_id = id;
        }else
            sel_id = table.getSelecteCheckboxValue("role_id");
        var dm = new Map();
        dm.put('role_id',"'"+sel_id.replace(/,/ig,"','")+"'");
        if(top.ajax.synclBody("/role/del",dm.toJSON()))
        {
            modelWindow.alert("角色信息"+top.CMSLang.Delete_success);
            loadListble();
        }else
        {
            modelWindow.error("角色信息"+top.CMSLang.Delete_fail);
        }
    }

    function deleteSingleObject(id)
    {
        modelWindow.confirm("是否删除该条记录","deleteObject('"+id+"')")
    }

    //保存角色写权限关联
    function saveRoleOperate(opt_ids)
    {
        var role_id = "";
        role_id = getCurrentSelectedBean().role_id;
        var role_map = new Map();
        role_map.put("role_id",role_id+"");
        role_map.put("opt_ids",opt_ids+"");
        if(top.ajax.synclBody("/roleOpt/insertRoleOpt",role_map.toJSON()))
        {
            modelWindow.alert("角色权限关联"+top.CMSLang.Add_success);
            table.unChekcbox();
        }else{
            modelWindow.error("角色权限关联"+top.CMSLang.Add_fail);
        }
    }

    var current_role_bean;
    function openReleOperateHandl(num,update_status)
    {
        current_role_bean = beanList[num];
        openSelectOperatePage('选择权限','saveRoleOperate',update_status);
    }

    //打开权限选择窗口
    function openSelectOperatePage(title,handl_name,update_status)
    {
        modelWindow.openFrameWindow(title,"/operate/go_select_operate?handle_name="+handl_name+"&update_status="+update_status,450,510);
    }

    function openReleUserHandl(num)
    {
        current_role_bean = beanList[num];
        modelWindow.openFrameWindow("选择用户","/userRole/go_select_user?role_id="+current_role_bean.role_id+"&handle_name=saveUser",750,530);
    }

    //用户角色关联
    var select_role_user_ids = "";
    function saveUser(user_ids){
        var insert_user_ids = removeAlreadyStr(user_ids,select_role_user_ids);//从ID中分析出新加的用户，已有的就不再加了
        var delete_user_ids = removeNotAlreadyStr(user_ids,select_role_user_ids);//从ID中分析出需要删除的用户

        var um = new Map();
        um.put('role_id',getCurrentSelectedBean().role_id);
        um.put('delete_user_ids',delete_user_ids);
        um.put('user_id',insert_user_ids);

        if(top.ajax.synclBody("/userRole/insertUserRoleByRole",um.toJSON()))
        {
            modelWindow.alert("角色用户关联"+top.CMSLang.Add_success);

        }else
        {
            modelWindow.error("角色用户关联"+top.CMSLang.Add_fail);
        }
    }

    //得到已选择过的权限ID
    function getSelectedRoleIDS()
    {
        var role_id = getCurrentSelectedBean().role_id;
        return top.ajax.syncl("/roleOpt/getOptIDSByRoleID/"+role_id,"").result;//RoleRPC.getOptIDSByRoleID(role_id);
    }

    function getCurrentSelectedBean()
    {
        return current_role_bean;
    }

    //字符排重，去掉第一个参数中已有的数据
    function removeAlreadyStr(str,old_str)
    {
        var arr1 = str.split(",");
        var arr2 = old_str.split(",");
        for (var i=arr1.length-1; i>=0; i--)
        {
            for ( var j=0; j<arr2.length; j++)
            {
                if (arr1[i]==arr2[j])
                    arr1.splice(i,1);
            }
        }
        return arr1.toString();
    }

    //字符排重，得到第一个参数没有的数据
    function removeNotAlreadyStr(str,old_str)
    {
        var arr1 = old_str.split(",");
        var arr2 = str.split(",");
        for (var i=arr1.length-1; i>=0; i--)
        {
            for ( var j=0; j<arr2.length; j++)
            {
                if (arr1[i]==arr2[j])
                    arr1.splice(i,1);
            }
        }
        return arr1.toString();
    }

    //得到该角色下已有的用户ID
    var select_role_user_ids = "";
    function getSelectedUserIDS()
    {
        var r_id = "";
        select_role_user_ids = "";

        r_id = current_role_bean.role_id;
        select_role_user_ids = top.ajax.syncl("/userRole/getUserIdsByRoleId/"+r_id,"").result;//RoleRPC.getUsersByRole(r_id,app_id,cnode_id);

        return select_role_user_ids;
    }
</script>
</body>
</html>
